/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aptech.mBean;

import com.aptech.entities.TblWorking;
import com.aptech.mBean.util.JsfUtil;
import com.aptech.sBean.TblWorkingFacade;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.faces.model.SelectItem;

/**
 *
 * @author LanhAnh
 */
@ManagedBean(name = "workingController")
@SessionScoped
public class WorkingController implements Serializable{

    @EJB
    private TblWorkingFacade tblWorkingFacade;

    public SelectItem[] getItemsAvailableSelectMany() {
        return JsfUtil.getSelectItems(tblWorkingFacade.findAll(), false);
    }

    public SelectItem[] getItemsAvailableSelectOne() {
        return JsfUtil.getSelectItems(tblWorkingFacade.findAll(), true);
    }
    
     @FacesConverter(forClass = TblWorking.class)
    public static class ElevationControllerConverter implements Converter {

        @Override
        public Object getAsObject(FacesContext context, UIComponent component, String value) {
            if (value == null || value.length() == 0) {
                return null;
            }
            WorkingController controller = (WorkingController) context.getApplication().getELResolver().
                    getValue(context.getELContext(), null, "workingController");
            return controller.tblWorkingFacade.find(getKey(value));
        }

        public String getAsString(FacesContext context, UIComponent component, Object object) {
            if (object == null) {
                return null;
            }
            if (object instanceof TblWorking) {
                TblWorking o = (TblWorking) object;
                return getStringKey(o.getWkID());
            } else {
                throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + TblWorking.class.getName());
            }
        }

        java.lang.Integer getKey(String value) {
            java.lang.Integer key;
            key = Integer.valueOf(value);
            return key;
        }

        String getStringKey(java.lang.Integer value) {
            StringBuffer sb = new StringBuffer();
            sb.append(value);
            return sb.toString();
        }
    }

    /**
     * Creates a new instance of WorkingController
     */
    public WorkingController() {
    }
}
